package com.lg.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.lg.model.AngelCandidate;
import com.lg.model.exception.ContentTypeNotSupportException;
import com.lg.model.exception.FileIsNullException;
import com.lg.util.MultipartFileValidator;

@Controller
@RequestMapping("/upload.do")
public class FileUploadController extends BaseController {
	@Resource(name = "multipartFileValidator")
	private MultipartFileValidator multipartFileValidator;

	/**
	 * 上传file1方法
	 */
	@RequestMapping(params = "method=uploadfile1", method = RequestMethod.POST)
	public String uploadfile1(@RequestParam("file1")
	MultipartFile file1, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file1, "file1", candidate, request, response, model);
	}

	/**
	 * 上传file2方法
	 */
	@RequestMapping(params = "method=uploadfile2", method = RequestMethod.POST)
	public String uploadfile2(@RequestParam("file2")
	MultipartFile file2, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file2, "file2", candidate, request, response, model);
	}

	/**
	 * 上传file3方法
	 */
	@RequestMapping(params = "method=uploadfile3", method = RequestMethod.POST)
	public String uploadfile3(@RequestParam("file3")
	MultipartFile file3, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file3, "file3", candidate, request, response, model);
	}

	/**
	 * 上传file4方法
	 */
	@RequestMapping(params = "method=uploadfile4", method = RequestMethod.POST)
	public String uploadfile4(@RequestParam("file4")
	MultipartFile file4, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file4, "file4", candidate, request, response, model);
	}

	/**
	 * 上传file5方法
	 */
	@RequestMapping(params = "method=uploadfile5", method = RequestMethod.POST)
	public String uploadfile5(@RequestParam("file5")
	MultipartFile file5, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file5, "file5", candidate, request, response, model);
	}

	/**
	 * 上传file6方法
	 */
	@RequestMapping(params = "method=uploadfile6", method = RequestMethod.POST)
	public String uploadfile6(@RequestParam("file6")
	MultipartFile file6, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file6, "file6", candidate, request, response, model);
	}

	/**
	 * 上传file7方法
	 */
	@RequestMapping(params = "method=uploadfile7", method = RequestMethod.POST)
	public String uploadfile7(@RequestParam("file7")
	MultipartFile file7, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file7, "file7", candidate, request, response, model);
	}

	/**
	 * 上传file8方法
	 */
	@RequestMapping(params = "method=uploadfile8", method = RequestMethod.POST)
	public String uploadfile8(@RequestParam("file8")
	MultipartFile file8, AngelCandidate candidate,
			MultipartHttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		return upload(file8, "file8", candidate, request, response, model);
	}

	/**
	 * 上传处理类
	 * 
	 * @throws UnsupportedEncodingException
	 * 
	 * @throws IOException
	 */
	public String upload(MultipartFile file, String inputname,
			AngelCandidate candidate, MultipartHttpServletRequest request,
			HttpServletResponse response, Model model)
			throws UnsupportedEncodingException {
		try {
			multipartFileValidator.validate(file);
		} catch (FileIsNullException e) {
			model.addAttribute("msg", e.getMessage());
			model.addAttribute("candidate", candidate);
			return "completedata";
		} catch (ContentTypeNotSupportException e) {
			model.addAttribute("msg", e.getMessage());
			model.addAttribute("candidate", candidate);
			return "completedata";
		}
		String uploadDir = request.getSession().getServletContext()
				.getRealPath("/")
				+ this.multipartFileValidator.getFilePath();
		String sep = System.getProperty("file.separator");
		File dirPath = new File(uploadDir);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}
		Date date = new Date();
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddhhmmssS");
		String fileName = new String(formatter1.format(date));
		String originalFilename = file.getOriginalFilename();
		String ext = originalFilename.substring(
				originalFilename.lastIndexOf(".") + 1,
				originalFilename.length()).toLowerCase();
		File uploadedFile = new File(uploadDir + sep + fileName + "." + ext);
		try {
			FileCopyUtils.copy(file.getBytes(), uploadedFile);
		} catch (IOException e) {
			model.addAttribute("msg", e.getMessage());
			model.addAttribute("candidate", candidate);
			return "completedata";
		}
		if (inputname == "file1") {
			candidate.setImagefile1(fileName + "." + ext);
		} else if (inputname == "file2") {
			candidate.setImagefile2(fileName + "." + ext);
		} else if (inputname == "file3") {
			candidate.setImagefile3(fileName + "." + ext);
		} else if (inputname == "file4") {
			candidate.setImagefile4(fileName + "." + ext);
		} else if (inputname == "file5") {
			candidate.setImagefile5(fileName + "." + ext);
		} else if (inputname == "file6") {
			candidate.setImagefile6(fileName + "." + ext);
		} else if (inputname == "file7") {
			candidate.setImagefile7(fileName + "." + ext);
		} else if (inputname == "file8") {
			candidate.setImagefile8(fileName + "." + ext);
		}
		model.addAttribute("msg", "文件上传成功!");
		model.addAttribute("candidate", candidate);
		return "completedata";
	}
}
